home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7104 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  56 lines

  1. Path: ra.cgd.ucar.edu!rosinski
  2. From: rosinski@ra.cgd.ucar.edu (Jim Rosinski)
  3. Newsgroups: comp.lang.c
  4. Subject: Why does this work?
  5. Date: 19 Feb 1996 00:11:36 GMT
  6. Organization: Climate and Global Dynamics Division/NCAR, Boulder, CO
  7. Message-ID: <4g8f7o$adt@ncar.ucar.edu>
  8. NNTP-Posting-Host: ra.cgd.ucar.edu
  9.  
  10.  
  11. Could someone please explain why the following code works?  In particular, I
  12. am perplexed as to why "sub1" and "sub2" declared as elements of "cmndtable"
  13. are valid "pointers to function returning void" (i.e. see the definition of
  14. "cmndstruct").  When executed, the net result is indeed to invoke two
  15. functions, "sub1", and "sub2" (tested on Solaris, AIX, and UNICOS).
  16.  
  17. Feel free to post, but please also email as I am not a regular reader of this
  18. group.  Thanks.
  19.  
  20. Jim Rosinski
  21. rosinski@ncar.ucar.edu
  22.  
  23.  
  24. #include <stdio.h>
  25. main()
  26. {
  27.   void sub1(), sub2();
  28.  
  29.   struct cmndstruct {
  30.     void (*funcnam)();
  31.   };
  32.  
  33.   struct cmndstruct cmndtable[] = {
  34.     sub1,
  35.     sub2,
  36.     NULL
  37.   };
  38.   struct cmndstruct *cmndptr;
  39.  
  40.   for (cmndptr = cmndtable; *(cmndptr->funcnam) != NULL; cmndptr++) {
  41.     (*(cmndptr->funcnam))();
  42.   }
  43.   exit(0);
  44. }
  45.  
  46. void sub1()
  47. {
  48.   printf("Inside sub1\n");
  49. }
  50.  
  51. void sub2()
  52. {
  53.   printf("Inside sub2\n");
  54. }
  55.  
  56.